<?php
$include_path = "../";
include_once("../util/db_manager.php");
require_once("admin_data_base_functions.php");
$error = false;
$msg = "";
$total = 0;
class Email {
	var $email_id;
	var $ip_address;
	var $name;
	var $reply_to;
	var $sbj;
	var $message;
	var $dt_time;
	var $mark_read;
	
	public static function getEmailList($result) {
		$emails = array();
		while ($row = mysql_fetch_array($result)) {
			$email = new Email();
			$email->email_id = $row["email_id"];
			$email->ip_address = $row["ip_address"];
			$email->name = $row["name"];
			$email->reply_to = $row["reply_to"];
			$email->sbj = $row["sbj"];
			$email->message = $row["message"];
			$email->dt_time = $row["dt"];
			$email->mark_read = $row["mark_read"];
			
			$emails[] = $email;
		}
		return $emails;
	}
	
	public static function markRead($email_id) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "UPDATE emails SET mark_read=1 WHERE email_id=" . $email_id;
			$dbManager->processQuery($query,$conn);
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
		$response = getResponseStart($GLOBALS['error'], $GLOBALS['msg'], $GLOBALS['total']);
		$response = $response . getResponseEnd();
		return $response;
	}
	
	public static function deleteMessages($email_ids) {
		if ($email_ids == "") {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = "No emails selected to delete";
		} else {
			try {
				//start db transaction
				$dbManager = DBManager::getInstance($GLOBALS['config_file']);
				$conn = $dbManager->fetchConnection();
				
				$query = "UPDATE emails SET deleted=1 WHERE email_id IN(" . $email_ids . ")";
				$dbManager->processQuery($query,$conn);
				$dbManager->releaseConnection($conn);
				$GLOBALS['msg'] = "completed";
			} catch (Exception $e) {
				$GLOBALS['error'] = true;
				$GLOBALS['msg'] = $e->getMessage();
			} 
		}
		$response = getResponseStart($GLOBALS['error'], $GLOBALS['msg'], $GLOBALS['total']);
		$response = $response . getResponseEnd();
		return $response;
	}
	
	public static function getMessages($page_no) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "SELECT email_id, ip_address, name, reply_to, sbj, message, DATE_FORMAT(dt_time, '%W, %b %e, %Y @ %h:%i %p') 'dt', mark_read FROM emails " .
						"WHERE deleted=0 " .
						"ORDER BY email_id DESC LIMIT " . ($page_no*20 - 20) . ",20";
			$result = $dbManager->processQuery($query,$conn);
			$emails = Email::getEmailList($result);
			$query = "SELECT count(1) 'total' FROM emails " .
						"WHERE deleted=0";
			$result = $dbManager->processQuery($query,$conn);
			while ($row = mysql_fetch_array($result)) {
				$GLOBALS['total'] = CEIL(intval($row["total"])/20);
			}
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
		return Email::getEmailResponse($emails);
	}

	public static function getEmailResponse($emails) {
		$response = getResponseStart($GLOBALS['error'], $GLOBALS['msg'], $GLOBALS['total']);
		$response = $response . "<emails>";
		if ($emails != null) {
			for ($cntr = 0; $cntr < count($emails); $cntr++) {
				$email = $emails[$cntr];
				$response = $response .  "<email><id>";
				$response = $response .  $email->email_id;
				$response = $response .  "</id><ip-address>";
				$response = $response .  $email->ip_address;
				$response = $response .  "</ip-address><name>";
				$response = $response .  "<![CDATA[" . $email->name . "]]>";
				$response = $response .  "</name><reply-to>";
				$response = $response .  "<![CDATA[" . $email->reply_to . "]]>";
				$response = $response .  "</reply-to><sbj>";
				$response = $response .  "<![CDATA[" . $email->sbj . "]]>";
				$response = $response .  "</sbj><message>";
				$response = $response .  "<![CDATA[" . $email->message . "]]>";
				$response = $response .  "</message><dt-time>";
				$response = $response .  $email->dt_time;
				$response = $response .  "</dt-time><mark-read>";
				$response = $response .  $email->mark_read;
				$response = $response .  "</mark-read></email>";
			}
		}
		$response = $response . "</emails>";
		$response = $response . getResponseEnd();
		return $response;
	}
}
?>
